<template>
    <h2 style="text-align: center;">医生信息展示</h2>
    <a href="/AddDoctor" class="btn btn-success">添加医生信息</a><br>
    医生名称:<input type="text" placeholder="请输入医生名称" v-model="name" style="margin-right: 10px;" />
    职称:<select v-model="Professional_Id" style="margin-right: 10px;" >
            <option :value="-1">请选择</option>
            <option v-for="item in InfoPro" :value="item.Professional_Id">{{ item.Professional_Name }}</option>
        </select>
    <input type="button" value="查询" class="btn btn-success"  @click="ShowDoctor" style="margin-right: 20px;"/>
    <input type="button" value="导出" class="btn btn-success" @click="ExprotFpath" style="margin-right: 10px;"/>
    <input type="button" value="导入" class="btn btn-success" @click="ImprotFpath" />
    <table border="1" class="table table-bordered">
        <thead style="background-color: aquamarine;">
            <tr style="text-align: center;">
                <th>医生编号</th>
                <th>医生图片</th>
                <th>医生名称</th>
                <th>所属医院</th>
                <th>医生学历</th>
                <th>所属科室</th>
                <th>职称</th>
                <th>操作</th>
            </tr>
        </thead>
            <tbody style="text-align: center;">
            <tr v-for="item in Info">
                <th>{{ item.Doctor_Id }}</th>
                <th><img :src="item.Doctor_Img" style="width: 60px;height: 60px;"/></th>
                <th>{{ item.Doctor_Name }}</th>
                <th>{{ item.Doctor_Adress }}</th>
                <th>{{ item.Doctor_Educational }}</th>
                <th>{{ item.Dept_Name }}</th>
                <th>{{ item.Professional_Name }}</th>
                <th>
                    <a @click="UpdDoctor(item.Doctor_Id)" class="btn btn-success" style="margin-right: 10px;">详情</a>
                    <a @click="DelDoctor(item.Doctor_Id)" class="btn btn-danger">删除</a>
                </th>
            </tr>
        </tbody>
    </table>
    <span>
        数据共{{ PageInfo.zongtiao }}条，每页{{ PageInfo.size}}条，共{{ PageInfo.index }}/{{ PageInfo.zongpage }}页

        <a @click="FanYe('F')" class="btn btn-success" style="margin-right: 10px;">首页</a>
        <a @click="FanYe('U')" class="btn btn-success" style="margin-right: 10px;">上一页</a>
        <a @click="FanYe('N')" class="btn btn-success" style="margin-right: 10px;">下一页</a>
        <a @click="FanYe('E')" class="btn btn-success" style="margin-right: 10px;">尾页</a>
    </span>
</template>


<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';
    import { useRouter } from 'vue-router';
    import { useCounterStore } from '@/stores/counter';

    const router=useRouter();
    const store=useCounterStore();
    if(store.Userid=="")
    {
        alert('请选登录');
        router.push("/Login");
    }
    //显示响应式数据
    const Info=ref([{
        Doctor_Id: 0,
        Doctor_Name: "",
        Doctor_Adress: "",
        Doctor_Img: "",
        Doctor_Educational: "",
        Dept_Id: 0,
        Dept_Name: "",
        Professional_Id: 0,
        Professional_Name: ""
    }])
    
    //职称响应式数据
    const InfoPro=ref([{
        Professional_Id: 0,
        Professional_Name: ""
    }])
    //分页响应式数据
    const PageInfo=ref({
        index:1,
        size:2,
        zongtiao:0,
        zongpage:0
    })
    //分页
    const FanYe=(chooes:string)=>{
        switch(chooes)
        {
            case"F":
                PageInfo.value.index=1;
            break;
            case"U":
                if(PageInfo.value.index>1)
                {
                    PageInfo.value.index--;
                }
                else
                {
                    alert('当前已是第一页');
                    return;
                }
            break;
            case"N":
                if(PageInfo.value.index<PageInfo.value.zongpage)
                {
                    PageInfo.value.index++;
                }
                else
                {
                    alert('当前已是最后一页');
                    return;
                }
            break;
            case"E":
                PageInfo.value.index=PageInfo.value.zongpage;
            break;
        }
        ShowDoctor();//跳转到显示页面
    }
    //文档就绪函数
    onMounted(()=>{
        GetProfessionals();//获取职称信息
        ShowDoctor();//显示
    })
    //获取职称信息
    const GetProfessionals=()=>{
        axios.get("https://localhost:44306/api/Doctor/GetProfessionals")
        .then(res=>{
            InfoPro.value=res.data
        })
    }
    const name=ref("");
    const Professional_Id=ref(-1);
    //显示
    const ShowDoctor=()=>{
        axios.get("https://localhost:44306/api/Doctor/ShowDoctor",
            {
                params:{
                    name:name.value,
                    Professional_Id:Professional_Id.value,
                    index:PageInfo.value.index,
                    size:PageInfo.value.size
                }
            }
        )
        .then(res=>{
            Info.value=res.data.Plist,
            PageInfo.value.zongpage=res.data.ZongPage,
            PageInfo.value.zongtiao=res.data.ZongTiao
        })
    }
    //删除
    const DelDoctor=(Doctor_Id:number)=>{
        if(confirm("确定删除?")){
            axios.delete("https://localhost:44306/api/Doctor/DelDoctor?id="+Doctor_Id)
            .then(res=>{
                if(res.data>0)
                {
                    alert('删除成功');
                    ShowDoctor();//跳转到显示页面
                }
                else
                {
                    alert('删除失败');
                }
            })
        }
    }
    //详情
    const UpdDoctor=(Doctor_Id:number)=>{
        router.push({
            name:"UpdDoctor",
            params:{
                id:Doctor_Id
            }
        })
    }
    //导出  "E://桌面/月考复习/第六套/DoctorInfo.xlsx"
    const ExprotFpath=()=>{
        axios.post("https://localhost:44306/api/Doctor/ExprotFpath?Fpath="+"E://桌面/月考复习/第六套/DoctorInfo.xlsx")
        .then(res=>{
            if(res.data>0)
            {
                alert('导出成功');
            }
            else
            {
                alert('导出失败');
            }
        })
    }
    //导出  "E://桌面/月考复习/第六套/DoctorInfo.xlsx"
    const ImprotFpath=()=>{
        axios.post("https://localhost:44306/api/Doctor/ImprotFpath?Fpath="+"E://桌面/月考复习/第六套/DoctorInfo.xlsx")
        .then(res=>{
            if(res.data>0)
            {
                alert('导入成功');
            }
            else
            {
                alert('导入失败');
            }
        })
    }
</script>